clear all

log using "/Users/Didac/Dropbox/Shared/Replication Files/IO_Replication/Data_building_part_1_print.smcl", replace

* Set the Path
cd "/Users/Didac/Dropbox/Shared/Replication Files/IO_Replication/"

use "war_default_dataset.dta", clear

*******************************************
* WAR + EXTERNAL DEFAULT
********************************************

* Number of years at war between 1816-1913
bysort cowcode: egen years_external = total(external_war)

* War and Access
bysort cowcode: gen war_and_access_abroad = 0
bysort cowcode: replace war_and_access_abroad = 1 if external_war == 1 & external_sdv == 0
bysort cowcode: egen ty_war_and_external_kap_access = total(war_and_access_abroad)

* War while being in default
bysort cowcode: gen war_and_default_abroad = 0
bysort cowcode: replace war_and_default_abroad = 1 if external_war == 1 & external_sdv == 1
bysort cowcode: egen ty_war_and_external_default = total(war_and_default_abroad)


*******************************************************
* WAR + DOMESTIC DEFAULT
*******************************************************

gen either_default = 0 if (external_sdv == 0 & domestic_sdc == 0)
replace either_default = 1 if (external_sdv == 1 | domestic_sdc == 1)

* War and Access
bysort cowcode: gen war_and_access_either = 0
bysort cowcode:replace war_and_access_either = 1 if external_war == 1 & either_default == 0
bysort cowcode: egen ty_war_and_either_kap_access = total(war_and_access_either)

* War while being in default
bysort cowcode: gen war_and_default_either = 0
bysort cowcode: replace war_and_default_either = 1 if external_war == 1 & either_default == 1
bysort cowcode: egen ty_war_and_either_default = total(war_and_default_either)

*******************************************************
* WAR + MONETARIZATION 
*******************************************************

gen inflation_with_lead = . 
replace inflation_with_lead = 1 if inflation_crisis == 1
	
tsset cowcode year
	
replace inflation_with_lead = 1 if l.inflation_crisis == 1
replace inflation_with_lead = 1 if l.l.inflation_crisis == 1
replace inflation_with_lead = 1 if l.l.l.inflation_crisis == 1
replace inflation_with_lead = 1 if l.l.l.l.inflation_crisis == 1
replace inflation_with_lead = 0 if inflation_with_lead == . 

* War while being in default AND monetarization
bysort cowcode: gen war_and_default_with_monet = 0
bysort cowcode: replace war_and_default_with_monet = 1 if external_war == 1 & external_sdv == 1 & inflation_with_lead == 1
bysort cowcode: egen ty_war_and_default_with_monet = total(war_and_default_with_monet)


* War while being in default AND NO monetarization
bysort cowcode: gen war_and_default_no_monet = 0
bysort cowcode: replace war_and_default_no_monet = 1 if external_war == 1 & external_sdv == 1 & inflation_with_lead == 0
bysort cowcode: egen ty_war_and_default_no_monet = total(war_and_default_no_monet)

	


**********************************************************************************
* FINANCIAL CRISIS IN THE UK, THE BANKER OF THE WORLD - UK  (Reinhart & Rogoff)
**********************************************************************************

* List of banking and stock market crises 
sort year
list year if banking_crisis >= 1 & country == "United Kingdom"
list year if stock_market_crash >= 1 & country == "United Kingdom"

********* FOUR YEAR PERSISTENCE (BANKING and STOCK MARKET CRISIS)

sort year
gen stop_4y = 0 
replace stop_4y = 1 if year >=1825 & year<=1829
replace stop_4y = 1 if year >=1837 & year<= 1844
replace stop_4y = 1 if year >=1847 & year<= 1854
replace stop_4y = 1 if year >=1857	& year<= 1861
replace stop_4y = 1 if year >=1865  & year<=1871
replace stop_4y = 1 if year >=1890 & year<= 1894

*** Add 1910, a stock market crisis
	replace stop_4y = 1 if year >=1910 & year<= 1914

*** Add 1873 crisis, not considered in Reinhart and Rogoff. 
	replace stop_4y = 1 if year >=1873 & year<= 1877


********* FIVE YEAR PERSISTENCE (BANKING and STOCK MARKET CRISIS)

sort year
gen stop_5y = 0 
replace stop_5y = 1 if year >=1825 & year<=1830
replace stop_5y = 1 if year >=1837 & year<= 1845
replace stop_5y = 1 if year >=1847 & year<= 1855
replace stop_5y = 1 if year >=1857 & year<= 1862
replace stop_5y = 1 if year >=1865 & year<=1872
replace stop_5y = 1 if year >=1873 & year<= 1878
replace stop_5y = 1 if year >=1890 & year<= 1895
replace stop_5y = 1 if year >=1910 & year<= 1915


********* SIX YEAR PERSISTENCE (BANKING and STOCK MARKET CRISIS)

sort year
gen stop_6y = 0 
replace stop_6y = 1 if year >=1825 & year<=1831
replace stop_6y = 1 if year >=1837 & year<= 1845
replace stop_6y = 1 if year >=1847 & year<= 1856
replace stop_6y = 1 if year >=1857 & year<= 1863
replace stop_6y = 1 if year >=1865 & year<=1873
replace stop_6y = 1 if year >=1873 & year<= 1879
replace stop_6y = 1 if year >=1890 & year<= 1896
replace stop_6y = 1 if year >=1910 & year<= 1916
 

********************************************
* EXTERNAL WARS and CREDIT CRUNCH IN BRITAIN
********************************************

**** 4 Year Window

* War while credit flows
	bysort cowcode: gen war_and_flow_4y = 0
	bysort cowcode:replace war_and_flow_4y = 1 if external_war == 1 & stop_4y == 0
	bysort cowcode: egen total_years_war_and_flow_4y = total(war_and_flow_4y)
	
* War while credit stops
	bysort cowcode: gen war_and_stop_4y = 0
	bysort cowcode: replace war_and_stop_4y = 1 if external_war == 1 & stop_4y == 1
	bysort cowcode: egen total_years_war_and_stop_4y = total(war_and_stop_4y)

	
**** 4 year window without 1873 crisis [for robustness]

gen stop_4y_no73 = stop_4y
replace stop_4y_no73 = 0 if year >=1873 & year<= 1877

* War and Access
	bysort cowcode: gen war_and_flow_4y_no73 = 0
	bysort cowcode:replace war_and_flow_4y_no73 = 1 if external_war == 1 & stop_4y_no73 == 0
	bysort cowcode: egen ty_war_and_flow_4y_no73 = total(war_and_flow_4y_no73)

* War while being in default
	bysort cowcode: gen war_and_stop_4y_no73 = 0
	bysort cowcode: replace war_and_stop_4y_no73 = 1 if external_war == 1 & stop_4y_no73 == 1
	bysort cowcode: egen ty_war_and_stop_4y_no73 = total(war_and_stop_4y_no73)
	

**** 4 year window without 1910 crisis [for robustness]

gen stop_4y_no1910 = stop_4y
replace stop_4y_no1910 = 0 if year >=1910 & year<= 1914

* War and Access
	bysort cowcode: gen war_and_flow_4y_no10 = 0
	bysort cowcode:replace war_and_flow_4y_no10 = 1 if external_war == 1 & stop_4y_no1910 == 0
	bysort cowcode: egen ty_war_and_flow_4y_no10 = total(war_and_flow_4y_no10)

* War while being in default
	bysort cowcode: gen war_and_stop_4y_no10 = 0
	bysort cowcode: replace war_and_stop_4y_no10 = 1 if external_war == 1 & stop_4y_no1910 == 1
	bysort cowcode: egen ty_war_and_stop_4y_no10 = total(war_and_stop_4y_no10)
	
	
	
**** 5 Year Window [for robustness]

* War while credit flows
	bysort cowcode: gen war_and_flow_5y = 0
	bysort cowcode:replace war_and_flow_5y = 1 if external_war == 1 & stop_5y == 0
	bysort cowcode: egen total_years_war_and_flow_5y = total(war_and_flow_5y)

* War while credit stops
	bysort cowcode: gen war_and_stop_5y = 0
	bysort cowcode: replace war_and_stop_5y = 1 if external_war == 1 & stop_5y == 1
	bysort cowcode: egen total_years_war_and_stop_5y = total(war_and_stop_5y)
	

**** 6 Year Window [for robustness]

* War while credit flows
	bysort cowcode: gen war_and_flow_6y = 0
	bysort cowcode:replace war_and_flow_6y = 1 if external_war == 1 & stop_6y == 0
	bysort cowcode: egen total_years_war_and_flow_6y = total(war_and_flow_6y)

* War while credit stops
	bysort cowcode: gen war_and_stop_6y = 0
	bysort cowcode: replace war_and_stop_6y = 1 if external_war == 1 & stop_6y == 1
	bysort cowcode: egen total_years_war_and_stop_6y = total(war_and_stop_6y)
	


		
*****************************************************
* Ongoing Wars
*****************************************************

* Step 1. Identify wars fought having access to credit in t but not t+1
bysort cowcode: gen EXOG_ongoingwar_and_stop_4y = 0
tsset cowcode year
sort cowcode year 
bysort cowcode: gen forward_stop = f.stop_4y
bysort cowcode: gen forward_external_war = f.external_war
bysort cowcode: replace EXOG_ongoingwar_and_stop_4y = 1 if (external_war == 1 & stop_4y == 0) & (forward_external_war == 1 & forward_stop == 1)

* Step 2. War that began before a stop - Indicator wariable
bysort id_state_and_war: egen ongoing_war = max(EXOG_ongoingwar_and_stop_4y)

* Step 3. Identify all the years for wars that began before stop
replace EXOG_ongoingwar_and_stop_4y = war_and_stop_4y if ongoing_war == 1
	
* Step 4: The next code does the following: it does not count any war that begins while the credit market is down, regardless of when it ends or what happens in between. 
bysort id_state_and_war: egen year_start = min(year)
replace year_start = . if id_state_and_war == .
bysort id_state_and_war: gen exclude = 0 
bysort id_state_and_war: replace exclude = 1 if external_war == 1 & stop_4y == 1 & year == year_start 
bysort id_state_and_war: egen exclude_war = max(exclude)
replace EXOG_ongoingwar_and_stop_4y = 0 if exclude_war == 1
sort cowcode year
bysort cowcode: egen total_years_ongoingwar_stop_4y = total(EXOG_ongoingwar_and_stop_4y)
sum total_years_ongoingwar_stop_4y



*******************************************
* WHERE WAS WAR FOUGHT
*******************************************

bysort country: gen at_war = 0
bysort country: replace at_war = 1 if (external_war == 1)
bysort country: egen total_years_at_war = total(at_war)

bysort country: gen war_at_home = 0 & at_war == 0
bysort country: replace war_at_home = 1 if at_war == 1 & country == location1
bysort country: replace war_at_home = 1 if at_war == 1 & country == location2
bysort country: replace war_at_home = 1 if at_war == 1 & country == location3
bysort country: replace war_at_home = 1 if at_war == 1 & country == location4
bysort country: replace war_at_home = 1 if at_war == 1 & country == location5

bysort country: gen war_abroad = 1 if at_war == 1
bysort country: replace war_abroad = 0 if at_war == 1 & (country == location1)
bysort country: replace war_abroad = 0 if at_war == 1 & (country == location2)
bysort country: replace war_abroad = 0 if at_war == 1 & (country == location3)
bysort country: replace war_abroad = 0 if at_war == 1 & (country == location4)
bysort country: replace war_abroad = 0 if at_war == 1 & (country == location5)

bysort country: egen total_years_war_at_home = total(war_at_home)
gen total_years_war_at_home_share = total_years_war_at_home/total_years_at_war
replace total_years_war_at_home_share = 0 if total_years_at_war == 0

bysort country: egen total_years_war_abroad = total(war_abroad)
gen total_years_war_abroad_share = total_years_war_abroad/total_years_at_war
replace total_years_war_abroad_share = 0 if total_years_at_war == 0

* New War Abroad
gen net_war_abroad = total_years_war_abroad - total_years_war_at_home


**********************
*  CIVIL WAR
**********************

* Civil war might be endogenous to external war.
* I recode civil war to 0 if it coincides in time with external war
replace civil_war = 0 if external_war == 1
* Sum of all civil war years fought in a country over the cyle. 
bysort cowcode: egen n_civil_wars = total(civil_war)

save "intermediate_step.dta", replace

****************************************
* AVERAGE DURATION OF INTER-STATE WAR
****************************************

use "intermediate_step.dta", clear

keep country cowcode id_state_and_war
drop if id_state_and_war == . 
bysort id_state_and_war: gen length = _N
replace length = . if id_state_and_war == .
drop if length == . 
bysort id_state_and_war: gen newid =_n
keep if newid==1
bysort country: egen mean_duration = mean(length)
bysort country: gen case = _n
keep if case == 1
keep cowcode mean_duration
save "mean_duration.dta", replace

use "intermediate_step.dta", clear
merge m:1 cowcode using "mean_duration.dta"
replace mean_duration = 0 if mean_duration == . & external_war == 0
drop _merge


****************************************************
*  TOTAL YEARS IN DEFAULT (Reinhart and Rogoff)
****************************************************

bysort cowcode: egen total_years_default = total(external_sdv)
save "country_year_format_main.dta", replace


****************************************
* KEEP ONE ENTRY PER COUNTRY 
****************************************

use "country_year_format_main.dta", clear

bysort cowcode: gen count = _n
keep if count == 1
drop count

keep country cowcode net_war_abroad mean_duration total_years_default total_years_ongoingwar_stop_4y total_years_war_and_flow_4y total_years_war_and_stop_4y ty_war_and_flow_4y_no73 ty_war_and_stop_4y_no73 total_years_war_and_flow_5y total_years_war_and_stop_5y total_years_war_and_flow_6y total_years_war_and_stop_6y ty_war_and_external_default rr_sample ty_war_and_external_kap_access ty_war_and_external_default ty_war_and_either_kap_access ty_war_and_either_default ty_war_and_default_with_monet ty_war_and_default_no_monet ty_war_and_stop_4y_no10 ty_war_and_flow_4y_no10 years_external n_civil

save "war_and_suddenstop_1816_1913.dta", replace


*************************************************
* MERGE SUDDEN STOP DATA WITH HOST OF CONTROLS
*************************************************

use "war_and_suddenstop_1816_1913.dta", clear

merge 1:1 cowcode using "all_controls.dta"
drop _merge

* Default Data for Analysis File
save "data_for_main_analysis.dta", replace


*******************************************************************
*******************************************************************
*******************************************************************
*******************************************************************
*******************************************************************
*******************************************************************
******* 
******* CREATE DATASET RESTRICTED TO SOVEREIGN COUNTRIES
******* 
*******************************************************************
*******************************************************************
*******************************************************************
*******************************************************************
*******************************************************************
*******************************************************************

clear all

cd "/Users/Didac/Dropbox/Shared/Replication Files/IO_Replication/"

use "war_default_dataset.dta", clear

*** FILTER no. 1. Drop Countries that are not sovereign by the time they go to war
	drop if sovereign == 0
	
***********************************************************
* CREDIT CRUNCH IN BRITAIN (Reinhart & Rogoff)
***********************************************************

sort year
gen stop_4y = 0 
replace stop_4y = 1 if year >=1825 & year<=1829
replace stop_4y = 1 if year >=1837 & year<= 1844
replace stop_4y = 1 if year >=1847 & year<= 1854
replace stop_4y = 1 if year >=1857	& year<= 1861
replace stop_4y = 1 if year >=1865  & year<=1871
replace stop_4y = 1 if year >=1890 & year<= 1894

*** Add 1910, a stock market crisis
	replace stop_4y = 1 if year >=1910 & year<= 1914

*** Add 1873 crisis, not considered in Reinhart and Rogoff. 
	replace stop_4y = 1 if year >=1873 & year<= 1877


********************************************
* EXTERNAL WARS and CREDIT CRUNCH IN BRITAIN
********************************************

**** 4 Year Window

* War while credit flows
bysort cowcode: gen war_and_flow_4y = 0
bysort cowcode:replace war_and_flow_4y = 1 if external_war == 1 & stop_4y == 0
bysort cowcode: egen total_years_war_and_flow_4y = total(war_and_flow_4y)

* War while credit stops
bysort cowcode: gen war_and_stop_4y = 0
bysort cowcode: replace war_and_stop_4y = 1 if external_war == 1 & stop_4y == 1
bysort cowcode: egen total_years_war_and_stop_4y = total(war_and_stop_4y)


****************************************
* KEEP ONE ENTRY PER COUNTRY 
****************************************

bysort cowcode: gen count = _n
keep if count == 1
drop count

*************************************************
* MERGE SUDDEN STOP DATA WITH HOST OF CONTROLS
*************************************************

merge 1:1 cowcode using "all_controls.dta"
drop _merge

keep country cowcode avg_pit_95_05 total_years_war_and_flow_4y total_years_war_and_stop_4y region colony_uk colony_iberia colony_other pop_dens_1820 oil_producer near_coast desert greatpower

save "data_for_main_analysis_sovereign.dta", replace




*******************************************************************
*******************************************************************
*******************************************************************
*******************************************************************
*******************************************************************
*******************************************************************
******* 
******* CREATE DATASET THAT INCLUDES SECESSIONIST WAR
*******
*******************************************************************
*******************************************************************
*******************************************************************
*******************************************************************
*******************************************************************
*******************************************************************

clear all

cd "/Users/Didac/Dropbox/Shared/Replication Files/IO_Replication/"

use "war_default_dataset.dta", clear

*** FILTER no. 2. Treat Secessionist War as External War
	replace external_war = 1 if secession_war == 1


***********************************************************
* CREDIT CRUNCH IN BRITAIN (Reinhart & Rogoff)
***********************************************************

sort year
gen stop_4y = 0 
replace stop_4y = 1 if year >=1825 & year<=1829
replace stop_4y = 1 if year >=1837 & year<= 1844
replace stop_4y = 1 if year >=1847 & year<= 1854
replace stop_4y = 1 if year >=1857	& year<= 1861
replace stop_4y = 1 if year >=1865  & year<=1871
replace stop_4y = 1 if year >=1890 & year<= 1894

*** Add 1910, a stock market crisis
	replace stop_4y = 1 if year >=1910 & year<= 1914

*** Add 1873 crisis, not considered in Reinhart and Rogoff. 
	replace stop_4y = 1 if year >=1873 & year<= 1877


********************************************
* EXTERNAL WARS and CREDIT CRUNCH IN BRITAIN
********************************************

**** 4 Year Window

* War while credit flows
	bysort cowcode: gen war_and_flow_4y = 0
	bysort cowcode:replace war_and_flow_4y = 1 if external_war == 1 & stop_4y == 0
	bysort cowcode: egen total_years_war_and_flow_4y = total(war_and_flow_4y)
	
* War while credit stops
	bysort cowcode: gen war_and_stop_4y = 0
	bysort cowcode: replace war_and_stop_4y = 1 if external_war == 1 & stop_4y == 1
	bysort cowcode: egen total_years_war_and_stop_4y = total(war_and_stop_4y)

save "country_year_format_secession.dta", replace


****************************************
* KEEP ONE ENTRY PER COUNTRY 
****************************************

use "country_year_format_secession.dta", clear

bysort cowcode: gen count = _n
keep if count == 1
drop count


*************************************************
* MERGE SUDDEN STOP DATA WITH HOST OF CONTROLS
*************************************************

merge 1:1 cowcode using "all_controls.dta"
drop _merge

keep country cowcode avg_pit_95_05 total_years_war_and_flow_4y total_years_war_and_stop_4y region colony_uk colony_iberia colony_other pop_dens_1820 oil_producer near_coast desert greatpower

save "data_for_main_analysis_secession.dta", replace



*******************************************************************
*******************************************************************
*******************************************************************
*******************************************************************
*******************************************************************
*******************************************************************
******* 
******* CREATE DATASET WHICH DROPS BRITISH WAR
*******
*******************************************************************
*******************************************************************
*******************************************************************
*******************************************************************
*******************************************************************
*******************************************************************

clear all

cd "/Users/Didac/Dropbox/Shared/Replication Files/IO_Replication/"

use "war_default_dataset.dta", clear

*** FILTER no. 3 Drop wars participated by Britain (as established by Wimmer and Min)
	drop if British_war == 1


***********************************************************
* CREDIT CRUNCH IN BRITAIN (Reinhart & Rogoff)
***********************************************************

* For list see Figure 
sort year
list year if banking_crisis >= 1 & country == "United Kingdom"
list year if stock_market_crash >= 1 & country == "United Kingdom"

********* FOUR YEAR PERSISTENCE (BANKING and STOCK MARKET CRISIS)

sort year
gen stop_4y = 0 
replace stop_4y = 1 if year >=1825 & year<=1829
replace stop_4y = 1 if year >=1837 & year<= 1844
replace stop_4y = 1 if year >=1847 & year<= 1854
replace stop_4y = 1 if year >=1857	& year<= 1861
replace stop_4y = 1 if year >=1865  & year<=1871
replace stop_4y = 1 if year >=1890 & year<= 1894

*** Add 1910, a stock market crisis
	replace stop_4y = 1 if year >=1910 & year<= 1914

*** Add 1873 crisis, not considered in Reinhart and Rogoff. 
	replace stop_4y = 1 if year >=1873 & year<= 1877


********************************************
* EXTERNAL WARS and CREDIT CRUNCH IN BRITAIN
********************************************

**** 4 Year Window

* War while credit flows
	bysort cowcode: gen war_and_flow_4y = 0
	bysort cowcode:replace war_and_flow_4y = 1 if external_war == 1 & stop_4y == 0
	bysort cowcode: egen total_years_war_and_flow_4y = total(war_and_flow_4y)
	
* War while credit stops
	bysort cowcode: gen war_and_stop_4y = 0
	bysort cowcode: replace war_and_stop_4y = 1 if external_war == 1 & stop_4y == 1
	bysort cowcode: egen total_years_war_and_stop_4y = total(war_and_stop_4y)


****************************************
* KEEP ONE ENTRY PER COUNTRY 
****************************************

bysort cowcode: gen count = _n
keep if count == 1
drop count

*************************************************
* MERGE SUDDEN STOP DATA WITH HOST OF CONTROLS
*************************************************

merge 1:1 cowcode using "all_controls.dta"
drop _merge

keep country cowcode avg_pit_95_05 total_years_war_and_flow_4y total_years_war_and_stop_4y region colony_uk colony_iberia colony_other pop_dens_1820 oil_producer near_coast desert greatpower statehist census_1820

save "data_for_main_analysis_nonBritishwar.dta", replace


**********************************************************
**********************************************************
**********************************************************
**********************************************************
**********************************************************
**********************************************************
*******
******* CREATE DATASET THAT INCLUDES TENTATIVE MATCHES
*******
*********************************************************
**********************************************************
**********************************************************
**********************************************************
**********************************************************
**********************************************************


clear all

cd "/Users/Didac/Dropbox/Shared/Replication Files/IO_Replication/"

use "war_default_data_with_tentative.dta", clear

***********************************************************
* CREDIT CRUNCH IN BRITAIN (Reinhart & Rogoff)
***********************************************************

sort year
gen stop_4y = 0 
replace stop_4y = 1 if year >=1825 & year<=1829
replace stop_4y = 1 if year >=1837 & year<= 1844
replace stop_4y = 1 if year >=1847 & year<= 1854
replace stop_4y = 1 if year >=1857	& year<= 1861
replace stop_4y = 1 if year >=1865  & year<=1871
replace stop_4y = 1 if year >=1890 & year<= 1894

*** Add 1910, a stock market crisis
	replace stop_4y = 1 if year >=1910 & year<= 1914

*** Add 1873 crisis, not considered in Reinhart and Rogoff. 
	replace stop_4y = 1 if year >=1873 & year<= 1877

********************************************
* EXTERNAL WARS and CREDIT CRUNCH IN BRITAIN
********************************************

**** 4 Year Window

* War while credit flows
	bysort cowcode: gen war_and_flow_4y = 0
	bysort cowcode:replace war_and_flow_4y = 1 if external_war == 1 & stop_4y == 0
	bysort cowcode: egen total_years_war_and_flow_4y = total(war_and_flow_4y)
	
* War while credit stops
	bysort cowcode: gen war_and_stop_4y = 0
	bysort cowcode: replace war_and_stop_4y = 1 if external_war == 1 & stop_4y == 1
	bysort cowcode: egen total_years_war_and_stop_4y = total(war_and_stop_4y)

****************************************
* KEEP ONE ENTRY PER COUNTRY 
****************************************

bysort cowcode: gen count = _n
keep if count == 1
drop count

merge 1:1 cowcode using "all_controls.dta"
drop _merge

keep country cowcode avg_pit_95_05 total_years_war_and_flow_4y total_years_war_and_stop_4y region colony_uk colony_iberia colony_other pop_dens_1820 oil_producer near_coast desert greatpower state census_1820 

save "data_for_main_analysis_with_tentative.dta", replace




**********************************************************
**********************************************************
**********************************************************
**********************************************************
**********************************************************
**********************************************************
*******
******* CREATE DATASET WITH WAR DATA DRAWN FROM CORRELATES 
******* OF WAR (COW v.4.0) RATHER THAN WIMMER AND MIN
*******
*********************************************************
**********************************************************
**********************************************************
**********************************************************
**********************************************************
**********************************************************


****************************************
* PART 1. ALL WAR BY COW 
****************************************

use "war_by_COW.dta", clear

/* CREDIT CRUNCH WINDOWS */

	gen stop_4y = 0 
	replace stop_4y = 1 if year >=1825 & year<=1829
	replace stop_4y = 1 if year >=1837 & year<= 1844
	replace stop_4y = 1 if year >=1847 & year<= 1854
	replace stop_4y = 1 if year >=1857	& year<= 1861
	replace stop_4y = 1 if year >=1865  & year<=1871
	replace stop_4y = 1 if year >=1890 & year<= 1894

*** Add 1910, a stock market crisis
	replace stop_4y = 1 if year >=1910 & year<= 1914

*** Add 1873 crisis, not considered in Reinhart and Rogoff. 
	replace stop_4y = 1 if year >=1873 & year<= 1877

save "country_year_format_main_COW.dta", replace

****** The following descriptives are summarized in the text
****** in Section 5.3 (there is no specific table)
 
	* # COW inter-state conflicts
		use "country_year_format_main_COW.dta", clear
		egen cow_war_county = group(inter_war_number_COW)
		tab cow_war_county
		* 37 COW inter-state conflicts
		
	* Total number of years and mean duration of war while credit flows with standard deviation: 
		use "country_year_format_main_COW.dta", clear
		keep if external_war == 1
		keep if stop_4y == 0
		bysort country inter_war_number_COW: gen mean_duration_credit = _n
		sum mean_duration_credit
		* War-years while credit flows: 78 
		* Average duration: 1.57 years 
	
	* Total number of year and mean duration of war while credit stops with standard deviation: 
		use "country_year_format_main_COW.dta", clear
		keep if external_war == 1
		keep if stop_4y == 1
		bysort country inter_war_number_COW: gen mean_duration_no_credit = _n
		sum mean_duration_no_credit
		* War-years while credit stops: 94 
		* Average duration: 1.23 years 

	* Total number of war-years: T
		use "country_year_format_main_COW.dta", clear
		keep if external_war == 1
		egen count_war_number = group(inter_war_number_COW)
		sum count_war_number
		* 172 inter-state war years
		* % of war-years while credit flows = 100*(78/172) = 45.34%
		* % of war-years while credit stops = 100*(94/172) = 54.65%



/* EXTERNAL WARS and CREDIT CRUNCH IN BRITAIN */

use "country_year_format_main_COW.dta", clear
	
* War and Access [4 year windows]
	bysort cowcode: gen war_and_flow_4y = 0
	bysort cowcode:replace war_and_flow_4y = 1 if external_war == 1 & stop_4y == 0
	bysort cowcode: egen total_years_war_and_flow_4y = total(war_and_flow_4y)
	
* War while being in default [4 year windows]
	bysort cowcode: gen war_and_stop_4y = 0
	bysort cowcode: replace war_and_stop_4y = 1 if external_war == 1 & stop_4y == 1
	bysort cowcode: egen total_years_war_and_stop_4y = total(war_and_stop_4y)
	
	sum total_years_war_and_stop_4y

/* Net Victory */
	
	gen war_won = 1 if inter_outcome == 1
	gen war_lost = 1 if inter_outcome == 2
	bysort cowcode: egen success = total(war_won) 
	bysort cowcode: egen failure = total(war_lost) 
	gen net_victory = success - failure 
	replace net_victory = 0 if net_victory == .
	
/* Keep one unit per country */

	bysort cowcode: gen count = _n
	keep if count == 1
	drop count

	keep country cowcode total_years_war_and_flow_4y total_years_war_and_stop_4y net_victory

/* MERGE SUDDEN STOP DATA WITH CONTROLS */

	merge 1:1 cowcode using "all_controls.dta"
	drop _merge

save "data_for_COW_full_sample.dta", replace 


****************************************
*  PART 2. WAR BY NONINITIATORS ONLY
****************************************

use "war_by_COW.dta", clear

/* Keep only wars in which the country was attacked (i.e. set war by initiator to 0) */
 replace external_war = 0 if inter_initiator == 1


/* SUDDEN-STOP WINDOWS */

	gen stop_4y = 0 
	replace stop_4y = 1 if year >=1825 & year<=1829
	replace stop_4y = 1 if year >=1837 & year<= 1844
	replace stop_4y = 1 if year >=1847 & year<= 1854
	replace stop_4y = 1 if year >=1857	& year<= 1861
	replace stop_4y = 1 if year >=1865  & year<=1871
	replace stop_4y = 1 if year >=1890 & year<= 1894

*** Add 1910, a stock market crisis
	replace stop_4y = 1 if year >=1910 & year<= 1914

*** Add 1873 crisis, not considered in Reinhart and Rogoff. 
	replace stop_4y = 1 if year >=1873 & year<= 1877


/* EXTERNAL WARS and CREDIT CRUNCH IN BRITAIN */
	
* War and Access [4 year windows]
	bysort cowcode: gen war_and_flow_4y = 0
	bysort cowcode:replace war_and_flow_4y = 1 if external_war == 1 & stop_4y == 0
	bysort cowcode: egen total_years_war_and_flow_4y = total(war_and_flow_4y)
	
* War while being in default [4 year windows]
	bysort cowcode: gen war_and_stop_4y = 0
	bysort cowcode: replace war_and_stop_4y = 1 if external_war == 1 & stop_4y == 1
	bysort cowcode: egen total_years_war_and_stop_4y = total(war_and_stop_4y)
	

/* Net Victory */
	
	gen war_won = 1 if inter_outcome == 1
	gen war_lost = 1 if inter_outcome == 2
	bysort cowcode: egen success = total(war_won) 
	bysort cowcode: egen failure = total(war_lost) 
	gen net_victory = success - failure 
	replace net_victory = 0 if net_victory == .
	
/* Keep one unit per country */

	bysort cowcode: gen count = _n
	keep if count == 1
	drop count

	keep country cowcode total_years_war_and_flow_4y total_years_war_and_stop_4y net_victory

/* MERGE SUDDEN STOP DATA WITH CONTROLS */

	merge 1:1 cowcode using "all_controls.dta"
	drop _merge

save "data_for_COW_noninitiators_sample.dta", replace 


**********************************************************
**********************************************************
**********************************************************
**********************************************************
**********************************************************
**********************************************************
*******
******* CREATE DATASET TO PRODUCE FIGURE A-3	
*******
*********************************************************
**********************************************************
**********************************************************
**********************************************************
**********************************************************
**********************************************************

* Figure A-3 includes tentative matches and secessionist war

clear all

cd "/Users/Didac/Dropbox/Shared/Replication Files/IO_Replication/"

use "war_default_data_with_tentative.dta", clear

**** Treat Secessionist War as External War
	replace external_war = 1 if secession_war == 1

**** Number of years at war between 1816-1913
	bysort cowcode: egen years_external = total(external_war)

	
/* CREDIT CRUNCH IN BRITAIN (Reinhart & Rogoff) */	
	
	gen stop_4y = 0 
	replace stop_4y = 1 if year >=1825 & year<=1829
	replace stop_4y = 1 if year >=1837 & year<= 1844
	replace stop_4y = 1 if year >=1847 & year<= 1854
	replace stop_4y = 1 if year >=1857	& year<= 1861
	replace stop_4y = 1 if year >=1865  & year<=1871
	replace stop_4y = 1 if year >=1890 & year<= 1894

*** Add 1910, a stock market crisis
	replace stop_4y = 1 if year >=1910 & year<= 1914

*** Add 1873 crisis, not considered in Reinhart and Rogoff. 
	replace stop_4y = 1 if year >=1873 & year<= 1877

/* EXTERNAL WARS and CREDIT CRUNCH IN BRITAIN */
 
**** 4 Year Window

* War while credit flows
	bysort cowcode: gen war_and_flow_4y = 0
	bysort cowcode:replace war_and_flow_4y = 1 if external_war == 1 & stop_4y == 0
	bysort cowcode: egen total_years_war_and_flow_4y = total(war_and_flow_4y)
	
* War while credit stops
	bysort cowcode: gen war_and_stop_4y = 0
	bysort cowcode: replace war_and_stop_4y = 1 if external_war == 1 & stop_4y == 1
	bysort cowcode: egen total_years_war_and_stop_4y = total(war_and_stop_4y)

/* KEEP ONE ENTRY PER COUNTRY */
 
	bysort cowcode: gen count = _n
	keep if count == 1
	drop count

	merge 1:1 cowcode using "all_controls.dta"
	drop _merge

	keep country cowcode avg_pit_95_05 total_years_war_and_flow_4y total_years_war_and_stop_4y region colony_uk colony_iberia colony_other pop_dens_1820 oil_producer near_coast desert greatpower state census_1820 years_external

save "data_for_main_analysis_with_tentative_and_secession.dta", replace

log close

